﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QLTTTH.Entity;
using QLTTTH.Config;
using System.Data;
using System.Data.SqlClient;

namespace QLTTTH.DAO
{
    class PhuTrachDAO:DAOBase
    {
        //TODO: phu trach lam chua on
        public DataSet GetAll()
        {
            return Select("select * from PhuTrach");
        }

        public PhuTrach GetById(String mapt)
        {
            PhuTrach pt = null;
            SqlConnection conn = null;
            try
            {
                conn = Config.Config.getConnection();
                conn.Open();
                SqlCommand commnad = new SqlCommand("select * from PhuTrach where MaPhuTrach = '" + mapt + "'", conn);
                SqlDataReader dataReader = commnad.ExecuteReader();
                if (dataReader.Read())
                {
                    pt = new PhuTrach();
                    pt.MaNV = dataReader.GetString(0);
                    pt.MaCT = dataReader.GetString(1);
                    return pt;
                }
            }
            catch (Exception ex)
            {

            }
            finally
            {
                conn.Close();

            }
            return pt;
        }

        public bool Insert(PhuTrach pt)
        {

            String sql = "INSERT INTO [QLVT].[dbo].[PhuTrach]([MaNV],[MaCT])"
               + "VALUES('" + pt.MaNV + "'," + pt.MaCT + ")";
            return Update(sql);
        }

        public bool DeleteById(String mapt)
        {
            return Update("delete from PhuTrach where MaPhuTrach = '" + mapt + "'");
        }

        public bool UpdateById(PhuTrach pt)
        {
            String sql = "update PhuTrach "
                 + "set TenPhuTrach = '" + pt.MaNV+ "'"
                 + " where MaPhuTrach =  '" + pt.MaCT + "'";
            return Update(sql);
        }
    }
}
